package jtmcraft.game;

import java.util.ArrayList;
import java.util.List;
import jtmcraft.game.units.GameUnit;
import jtmcraft.game.units.GameUnitFactory.GameUnitType;

/**
 *
 * @author milosz
 */
public abstract class Controller
{
    protected Player player;
    protected ArrayList<Player> enemies = new ArrayList<Player>();
    protected ArrayList<GameUnit> neutralGameUnits = new ArrayList<GameUnit>();
    
    protected Controller nextHandler;

    public ArrayList<Player> getEnemies()
    {
        return enemies;
    }

    public ArrayList<GameUnit> getNeutralGameUnits()
    {
        return neutralGameUnits;
    }

    public Player getPlayer()
    {
        return player;
    }

    public Controller setNextHandler(Controller actionHandler)
    {
        nextHandler = actionHandler;
        return actionHandler;
    }

    public void addResource( int resource )
    {
        if( null != nextHandler ) {
            nextHandler.addResource( resource );
        }
    }

    public void attack( GameUnit attacker, GameUnit defender, double radian, double i )
    {
        if( null != nextHandler ) {
            nextHandler.attack( attacker, defender, radian, i );
        }
    }

    public void hit( GameUnit attacker, GameUnit defender, final int power )
    {
        if( null != nextHandler ) {
            nextHandler.hit( attacker, defender, power );
        }
    }

    public void createMobileUnit( GameUnitType type, int posX, int posY )
    {
        if( null != nextHandler ) {
            nextHandler.createMobileUnit( type, posX, posY );
        }
    }

    public void createBuildingUnit( GameUnitType type, int posX, int posY )
    {
        if( null != nextHandler ) {
            nextHandler.createBuildingUnit( type, posX, posY );
        }
    }

    public void createEnemyPlayer( Player player )
    {
        if( null != nextHandler ) {
            nextHandler.createEnemyPlayer( player );
        }
    }

    public void createEnemyMobileUnit( GameUnitType type, int posX, int posY, int enemyID )
    {
        if( null != nextHandler ) {
            nextHandler.createEnemyMobileUnit( type, posX, posY, enemyID );
        }
    }

    public void createEnemyBuildingUnit( GameUnitType type, int posX, int posY, int enemyID )
    {
        if( null != nextHandler ) {
            nextHandler.createEnemyBuildingUnit( type, posX, posY, enemyID );
        }
    }

    public void createNeutralGameUnit( GameUnitType type, int posX, int posY  )
    {
        if( null != nextHandler ) {
            nextHandler.createNeutralGameUnit( type, posX, posY );
        }
    }

    public void destroyUnit( GameUnit unit )
    {
        if( null != nextHandler ) {
            nextHandler.destroyUnit( unit );
        }
    }

    public List<GameUnit> getAllBuildingUnits()
    {
        ArrayList<GameUnit> buildings = new ArrayList<GameUnit>();
        for( GameUnit gu : player.getBuildingUnitList() ) {
            buildings.add( gu );
        }
        for( Player p : enemies ) {
            for( GameUnit gu : p.getBuildingUnitList() ) {
                buildings.add( gu );
            }
        }
        for( GameUnit gu : neutralGameUnits ) {
            buildings.add( gu );
        }
        return buildings;
    }
}